﻿using Common.Communication;
using Common.Device.DeviceDb;
using Common.Device.Enums;
using Common.Device.Interfaces;
using Common.Tool.DataTools;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Documents;

namespace DianZiFuZai
{
    public class IT8813NiVisa : NiVisaBase, IDianZiFuZai
    {
        public string Name { get; set; }
        public IT8813NiVisa(UseDevice device) : base("USB0::0x2EC7::0x8800::" + device.parameter + "::INSTR", 1500)
        {
            Name = device.name;
        }
        public Result<double> ReadCurrentValue(DianZiFuZaiEnum.DianZiFuZaiMode mode)
        {
            lock (this)
            {
                string command = "";
                double value = 0f;
                Result<double> result = new Result<double>();
                AppendInfo(Name, $"读取当前负载{mode.ToString()}");
                command = CmdReadCurrentValue(mode);
                //发送指令并返回
                var re2 = WriteThenRead(command);
                if (!re2.IsSucceed || !double.TryParse(re2.Value, out value))
                {
                    result.IsSucceed = false;
                    result.Err = "读取当前负载失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                result.Value = value;
                if (mode == DianZiFuZaiEnum.DianZiFuZaiMode.电压)
                {
                    AppendResult(Name, DataConvert.ToUnit(result.Value, "F3") + "V");
                }
                if (mode == DianZiFuZaiEnum.DianZiFuZaiMode.电流)
                {
                    AppendResult(Name, DataConvert.ToUnit(result.Value, "F3") + "A");
                }
                if (mode == DianZiFuZaiEnum.DianZiFuZaiMode.功率)
                {
                    AppendResult(Name, DataConvert.ToUnit(result.Value, "F3") + "W");
                }
                if (mode == DianZiFuZaiEnum.DianZiFuZaiMode.电阻)
                {
                    AppendResult(Name, DataConvert.ToUnit(result.Value, "F3") + "Ω");
                }
                return result.EndTime();
            }
        }

        public Result<double> ReadSetValue(DianZiFuZaiEnum.DianZiFuZaiMode mode)
        {
            lock (this)
            {
                string command = "";
                double value = 0f;
                Result<double> result = new Result<double>();
                var write = new Result();
                var read = new Result<string>();
                AppendInfo(Name, $"读取设置负载{mode.ToString()}");
                command = CmdReadFuZaiValue(mode);
                //发送指令并返回
                read = WriteThenRead(command);
                if (!read.IsSucceed || !double.TryParse(read.Value, out value))
                {
                    result.IsSucceed = false;
                    result.Err = "读取设置负载失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                result.Value = value;
                if (mode == DianZiFuZaiEnum.DianZiFuZaiMode.电压)
                {
                    AppendResult(Name, DataConvert.ToUnit(result.Value, "F3") + "V");
                }
                if (mode == DianZiFuZaiEnum.DianZiFuZaiMode.电流)
                {
                    AppendResult(Name, DataConvert.ToUnit(result.Value, "F3") + "A");
                }
                if (mode == DianZiFuZaiEnum.DianZiFuZaiMode.功率)
                {
                    AppendResult(Name, DataConvert.ToUnit(result.Value, "F3") + "W");
                }
                if (mode == DianZiFuZaiEnum.DianZiFuZaiMode.电阻)
                {
                    AppendResult(Name, DataConvert.ToUnit(result.Value, "F3") + "Ω");
                }
                return result.EndTime();
            }
        }

        public Result<bool> SetOn(DianZiFuZaiEnum.DianZiFuZaiMode mode, double value)
        {
            lock (this)
            {
                string command = "";
                var result = new Result<bool>();
                var write = new Result();
                var read = new Result<string>();
                AppendInfo(Name, $"设置负载模式{mode.ToString()}");
                command = CmdSetFuZaiMode(mode);
                write = Write(command);
                if (!write.IsSucceed)
                {
                    result.IsSucceed = false;
                    result.Err = "设置负载模式失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                Thread.Sleep(100);
                command = CmdReadFuZaiMode;
                //发送指令并返回
                read = WriteThenRead(command);
                if (!read.IsSucceed || !read.Value.ToLower().Contains(CmdReadFuZaiModeOk(mode).ToLower()))
                {
                    result.IsSucceed = false;
                    result.Err = "设置负载模式失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                AppendInfo(Name, "设置负载模式成功");
                AppendInfo(Name, $"设置负载{mode.ToString()}{value}");
                command = CmdSetFuZaiValue(mode, value);
                write = Write(command);
                if (!write.IsSucceed)
                {
                    result.IsSucceed = false;
                    result.Err = "设置负载失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                Thread.Sleep(100);
                command = CmdReadFuZaiValue(mode);
                //发送指令并返回
                double va = 0;
                read = WriteThenRead(command);
                if (!read.IsSucceed || !double.TryParse(read.Value, out va) || va != value)
                {
                    result.IsSucceed = false;
                    result.Err = "设置负载失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                AppendInfo(Name, "设置负载成功");
                AppendInfo(Name, $"设置负载输入ON");
                command =  CmdSetInputOn;
                write = Write(command);
                if (!write.IsSucceed)
                {
                    result.IsSucceed = false;
                    result.Err = "设置负载输入失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                Thread.Sleep(100);
                command = CmdReadInput;
                //发送指令并返回
                read = WriteThenRead(command);
                if (!read.IsSucceed || !read.Value.Contains(CmdSetInputOnOk))
                {
                    result.IsSucceed = false;
                    result.Err = "设置负载输入失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                AppendInfo(Name, "设置负载输入成功");
                return result.EndTime();
            }
        }

        public Result<bool> SetOff()
        {
            lock (this)
            {
                string command = "";
                var result = new Result<bool>();
                var write = new Result();
                var read = new Result<string>();
                AppendInfo(Name, $"设置负载输入OFF");
                command = CmdSetInputOff;
                write = Write(command);
                if (!write.IsSucceed)
                {
                    result.IsSucceed = false;
                    result.Err = "设置负载输入失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                Thread.Sleep(100);
                command = CmdReadInput;
                //发送指令并返回
                read = WriteThenRead(command);
                if (!read.IsSucceed || !read.Value.Contains(CmdSetInputOffOk))
                {
                    result.IsSucceed = false;
                    result.Err = "设置负载输入失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                AppendInfo(Name, "设置负载输入成功");
                return result.EndTime();
            }
        }
        public double VolMin = 0;
        public double VolMax = 120.000f;
        public double CurMin = 0;
        public double CurMax = 20.0000f;
        public double WattMin = 0;
        public double WattMax = 120.000f;
        public double RMin = 0;
        public double RMax = 4000.000f;

        string CmdSetRemote => "SYSTem:REMote\r\n";
        string CmdSetInputOn=> $"INPut ON\r\n";
        string CmdSetInputOff => $"INPut OFF\r\n";
        string CmdReadInput => $"INPut?\r\n";
        string CmdSetInputOnOk => "1";
        string CmdSetInputOffOk => "0";
        string CmdSetFuZaiValue(DianZiFuZaiEnum.DianZiFuZaiMode mode, double value)
        {
            switch (mode)
            {
                case DianZiFuZaiEnum.DianZiFuZaiMode.电压: return $"VOLTage {value}\r\n";
                case DianZiFuZaiEnum.DianZiFuZaiMode.电流: return $"CURRent {value}\r\n";
                case DianZiFuZaiEnum.DianZiFuZaiMode.功率: return $"POWer {value}\r\n";
                case DianZiFuZaiEnum.DianZiFuZaiMode.电阻: return $"RESistance {value}\r\n";
                default:
                    return "";
            }
        }
        string CmdReadFuZaiValue(DianZiFuZaiEnum.DianZiFuZaiMode mode)
        {
            switch (mode)
            {
                case DianZiFuZaiEnum.DianZiFuZaiMode.电压: return $"VOLTage?\r\n";
                case DianZiFuZaiEnum.DianZiFuZaiMode.电流: return $"CURRent?\r\n";
                case DianZiFuZaiEnum.DianZiFuZaiMode.功率: return $"POWer?\r\n";
                case DianZiFuZaiEnum.DianZiFuZaiMode.电阻: return $"RESistance?\r\n";
                default:
                    return "";
            }
        }
        string CmdSetFuZaiMode(DianZiFuZaiEnum.DianZiFuZaiMode mode)
        {
            switch (mode)
            {
                case DianZiFuZaiEnum.DianZiFuZaiMode.电压: return "FUNCtion VOLTage\r\n";
                case DianZiFuZaiEnum.DianZiFuZaiMode.电流: return "FUNCtion CURRent\r\n";
                case DianZiFuZaiEnum.DianZiFuZaiMode.功率: return "FUNCtion POWer\r\n";
                case DianZiFuZaiEnum.DianZiFuZaiMode.电阻: return "FUNCtion RESistance\r\n";
                default:
                    return "";
            }
        }
        string CmdReadFuZaiMode => "FUNCtion?\r\n";
        string CmdReadFuZaiModeOk(DianZiFuZaiEnum.DianZiFuZaiMode mode)
        {
            switch (mode)
            {
                case DianZiFuZaiEnum.DianZiFuZaiMode.电压: return "VOLTage";
                case DianZiFuZaiEnum.DianZiFuZaiMode.电流: return "CURRent";
                case DianZiFuZaiEnum.DianZiFuZaiMode.功率: return "POWer";
                case DianZiFuZaiEnum.DianZiFuZaiMode.电阻: return "RESistance";
                default:
                    return "";
            }
        }
        string CmdReadCurrentValue(DianZiFuZaiEnum.DianZiFuZaiMode mode)
        {
            switch (mode)
            {
                case DianZiFuZaiEnum.DianZiFuZaiMode.电压: return "FETCh:VOLTage?\r\n";
                case DianZiFuZaiEnum.DianZiFuZaiMode.电流: return "FETCh:CURRent?\r\n";
                case DianZiFuZaiEnum.DianZiFuZaiMode.功率: return "FETCh:POWer?\r\n";
                default:
                    return "";
            }
        }
    }
}
